package demo.db.influxdb;

import com.influxdb.client.InfluxDBClient;
import com.influxdb.client.InfluxDBClientFactory;
import com.influxdb.client.WriteApiBlocking;
import com.influxdb.client.domain.WritePrecision;
import com.influxdb.client.write.Point;
import org.junit.Test;

import java.time.Instant;

/**
 * https://github.com/influxdata/influxdb-client-java#writes-and-queries-in-influxdb-2x
 */
public class InfluxdbClientWriteDemo {


    private static char[] token = "my-token".toCharArray();
    private static String org = "my-org";
    private static String bucket = "my-bucket";

    private static String majorUrl = "http://192.168.3.66:8086";
    private static String minorUrl = "http://192.168.3.66:8086";
    private static String username = "southgate";
    private static String password = "wmxPpAkoST18";
    private static String database = "southgate";

    /**
     * 写数据
     */
    @Test
    public void testWriteData() {

        InfluxDBClient influxDBClient = InfluxDBClientFactory.create("http://localhost:8086", token, org, bucket);

        //
        // Write data
        //
        WriteApiBlocking writeApi = influxDBClient.getWriteApiBlocking();

        //
        // Write by Data Point
        //
        Point point = Point.measurement("temperature")
                .addTag("location", "west")
                .addField("value", 55D)
                .time(Instant.now().toEpochMilli(), WritePrecision.MS);

        writeApi.writePoint(point);

        //
        // Write by LineProtocol
        //
        writeApi.writeRecord(WritePrecision.NS, "temperature,location=north value=60.0");

        //
        // Write by POJO
        //
        InfluxDbQueryDemo.Temperature temperature = new InfluxDbQueryDemo.Temperature();
        temperature.location = "south";
        temperature.value = 62D;
        temperature.time = Instant.now();

        writeApi.writeMeasurement(WritePrecision.NS, temperature);


    }
}
